package com.letwal.cmcvnd.mywallet.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.letwal.cmcvnd.R
import com.letwal.cmcvnd.adata.RevDataWallet

class AddWalletAdapter(private val dataList: List<RevDataWallet>) :
    RecyclerView.Adapter<AddWalletAdapter.ViewHolder>() {

    //点击事件
    private var mItemClickListener: (Int) -> Unit = {}

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var imageAdapterAddWallet: ImageView =
            view.findViewById(R.id.imageAdapterAddWallet)
        var textAdapterAddWalletName: TextView =
            view.findViewById(R.id.textAdapterAddWalletName)
        var textAdapterAddWalletTips: TextView =
            view.findViewById(R.id.textAdapterAddWalletTips)
        var imageAdapterAddWalletCheck: ImageView =
            view.findViewById(R.id.imageAdapterAddWalletCheck)
        var rlAdapterAddWalletContent: RelativeLayout =
            view.findViewById(R.id.rlAdapterAddWalletContent)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.view_adpter_add_wallet, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data: RevDataWallet = dataList[position]
        holder.textAdapterAddWalletName.text = data.name
        holder.textAdapterAddWalletTips.text = data.detail

        if (data.checked) {
            holder.imageAdapterAddWalletCheck.setImageResource(R.drawable.icon_checked)
        } else {
            holder.imageAdapterAddWalletCheck.setImageResource(R.drawable.icon_uncheck)
        }

        //点击事件监听
        holder.rlAdapterAddWalletContent.setOnClickListener {
            mItemClickListener.invoke(position)
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    /** 设置点击事件 */
    fun setItemClickListener(listener: (Int) -> Unit) {
        mItemClickListener = listener
    }
}